
/* GCSx
** SCENEEDIT.H
**
** Scene support
** Expands basic Scene to include editor functionality
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#ifndef __GCSx_SCENEEDIT_H_
#define __GCSx_SCENEEDIT_H_

class SceneEdit : public Scene, virtual public SaveLoad {
protected:
    // Has the scene been modified from initial state (from empty if new)
    int headerModified;
    int contentModified;
    int disassociated;

    class LayerEdit* layerEdits[MAX_LAYERS]; // CACHEABLE

    // Our node on the world browser
    TreeView* browserNode;
    
    const static std::string tileButton;
    const static std::string imageButton;
    const static std::string fontButton;
    const static std::string addLayerButton;

public:
    // Id of 0 = we're going to load immediately
    // Any other id = we start out modified
    // World and ID may be NULL/0 if in process of creating
    SceneEdit(class WorldEdit* myWorld, int myId = 0); // Starts with default settings
    virtual ~SceneEdit();

    // Intended only for use after creating/importing- not in general usage
    void setInfo(class WorldEdit* myWorld, int newId);

    class WorldEdit* getWorldEdit() { return dynamic_cast<WorldEdit*>(world); }
    // (asserts pos is valid)
    // markLock() this scene as long as you're using the layer for nontrivial purposes
    class LayerEdit* getLayerEdit(int pos);
    
    // Layers
    // Asserts that position is valid and we aren't full on layers yet
    // Inserts at position- 0 is top layer; takes ownership of pointer
    // Locks layer to match our lock count
    // Returns >0 if successfully inserted
    // Returns <0 if successfully inserted without undo (you should delete if you cancel)
    // Returns 0 if not inserted and you should delete the layer
    int insertLayer(class LayerEdit* newLayer, int pos, Window* srcWin = NULL, Window* exWin = NULL, int readd = 0) throw_File;
    // Unlocks layer if locked; returns true if successfully deleted
    int deleteLayer(int pos, Window* srcWin = NULL, Window* exWin = NULL) throw_File;
    // Just swaps position of two layers
    void swapLayer(int pos1, int pos2, Window* srcWin = NULL, Window* exWin = NULL) throw_Undo;
    // Inserts an entirely new layer; calls properties dialog; returns true if one was inserted
    int newLayer(Window* srcWin = NULL, Window* exWin = NULL) throw_File;
    
    // Change properties; handles undo
    void setName(const std::string& newName, Window* srcWin = NULL, Window* exWin = NULL) throw_Undo;
    
    // Returns true if OK pressed
    int propertiesDialog(Window* srcWin = NULL, Window* exWin = NULL);
    
    // Edit window (editor) Adds a layer if none
    void openEditWindow();

    // Tells scene to add itself to the world browser node
    void addToBrowser(TreeView* node, int makeCurrent = 1);
    void dropBrowser(); // Called if scene gets dropped from browser

    // Tells us to disassociate ourselves from anything, we've been "deleted"
    // or that we've been "readded" again
    // FileException means couldn't decache, and nothing was done
    void disassociate() throw_File;
    // Node is world browser node that we readd to
    void reassociate(TreeView* node);

    // Treeview event handling
    int treeviewEvent(int code, int command, int check);
    static int treeviewEventWrap(void* ptr, int code, int command, int check);

    // Set us as modified; public so layers can access this
    // Setting content modified also loads from cache if needed
    // Call these before actually making any modifications
    // (if header changes affect content, call both modifieds first)
    void setHeaderModified();
    void setContentModified();

    // File access (recurses to layers)
    void loadHeader(FileRead* file) throw_File;
    int isHeaderModified() const;
    int isContentModified() const;
    Uint32 saveHeader(FileWrite* file) throw_File;
    void saveContent(FileWrite* file) throw_File;
    void saveSuccess();
    void cachedContent(FileRead* file, int oldData);
};

#endif

